package org.dfzt.modules.online.config.service.a;

import org.apache.commons.lang.StringUtils;
import org.dfzt.modules.online.config.exception.DBException;
import org.dfzt.modules.online.config.service.DbTableHandleI;
import org.dfzt.modules.online.config.util.a;


public class PostgresqlHander
        implements DbTableHandleI {
    public String getAddColumnSql(a columnMeta) {
        return " ADD COLUMN " + a(columnMeta) + ";";
    }


    public String getReNameFieldName(a columnMeta) {
        return " RENAME  COLUMN  " + columnMeta.getOldColumnName() + " to " + columnMeta.getColumnName() + ";";
    }


    public String getUpdateColumnSql(a cgformcolumnMeta, a datacolumnMeta) throws DBException {
        return "  ALTER  COLUMN   " + a(cgformcolumnMeta, datacolumnMeta) + ";";
    }


    public String getSpecialHandle(a cgformcolumnMeta, a datacolumnMeta) throws DBException {
        return "  ALTER  COLUMN   " + b(cgformcolumnMeta, datacolumnMeta) + ";";
    }


    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String str = "";
        if (dataType.equalsIgnoreCase("varchar")) {
            str = "string";
        } else if (dataType.equalsIgnoreCase("double")) {
            str = "double";
        } else if (dataType.contains("int")) {
            str = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("timestamp")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("bytea")) {
            str = "blob";
        } else if (dataType.equalsIgnoreCase("text")) {
            str = "text";
        } else if (dataType.equalsIgnoreCase("decimal")) {
            str = "bigdecimal";
        } else if (dataType.equalsIgnoreCase("numeric")) {

            str = "bigdecimal";
        }
        return str;
    }


    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName + " ;";
    }


    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }


    private String a(a parama1, a parama2) throws DBException {
        String str = "";

        if (parama1.getColunmType().equalsIgnoreCase("string")) {
            str = parama1.getColumnName() + "  type character varying(" + parama1.getColumnSize() + ") ";
        } else if (parama1.getColunmType().equalsIgnoreCase("date")) {
            str = parama1.getColumnName() + "  type timestamp ";
        } else if (parama1.getColunmType().equalsIgnoreCase("int")) {

            str = parama1.getColumnName() + " type int4 ";
        } else if (parama1.getColunmType().equalsIgnoreCase("double")) {
            str = parama1.getColumnName() + " type  numeric(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") ";
        } else if (parama1.getColunmType().equalsIgnoreCase("BigDecimal")) {
            str = parama1.getColumnName() + " type  decimal(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") ";
        } else if (parama1.getColunmType().equalsIgnoreCase("text")) {

            str = parama1.getColumnName() + " text ";
        } else if (parama1.getColunmType().equalsIgnoreCase("blob")) {

            throw new DBException("blob类型不可修改");
        }
        return str;
    }

    private String b(a parama1, a parama2) throws DBException {
        String str = "";

        if (!parama1.a(parama2)) {
            if (parama1.getColunmType().equalsIgnoreCase("string")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            } else if (parama1.getColunmType().equalsIgnoreCase("date")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            } else if (parama1.getColunmType().equalsIgnoreCase("int")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            } else if (parama1.getColunmType().equalsIgnoreCase("double")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            } else if (parama1.getColunmType().equalsIgnoreCase("bigdecimal")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            } else if (parama1.getColunmType().equalsIgnoreCase("text")) {
                str = parama1.getColumnName();
                str = str + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" SET DEFAULT " + parama1.getFieldDefault()) : " DROP DEFAULT");
            }
        }

        return str;
    }


    private String a(a parama) {
        String a = "";
        if (parama.getColunmType().equalsIgnoreCase("string")) {
            a = parama.getColumnName() + " character varying(" + parama.getColumnSize() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("date")) {
            a = parama.getColumnName() + " timestamp ";
        } else if (parama.getColunmType().equalsIgnoreCase("int")) {
            a = parama.getColumnName() + " int4";
        } else if (parama.getColunmType().equalsIgnoreCase("double")) {
            a = parama.getColumnName() + " numeric(" + parama.getColumnSize() + "," + parama.getDecimalDigits() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("bigdecimal")) {
            a = parama.getColumnName() + " decimal(" + parama.getColumnSize() + "," + parama.getDecimalDigits() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("blob")) {
            a = parama.getColumnName() + " bytea(" + parama.getColumnSize() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("text")) {

            a = parama.getColumnName() + " text ";
        }

        return a + (StringUtils.isNotEmpty(parama.getFieldDefault()) ? (" DEFAULT " + parama.getFieldDefault()) : " ");
    }


    private String b(a parama) {
        String str = "";
        if (parama.getColunmType().equalsIgnoreCase("string")) {
            str = parama.getColumnName() + " character varying(" + parama.getColumnSize() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("date")) {
            str = parama.getColumnName() + " datetime ";
        } else if (parama.getColunmType().equalsIgnoreCase("int")) {
            str = parama.getColumnName() + " int(" + parama.getColumnSize() + ") ";
        } else if (parama.getColunmType().equalsIgnoreCase("double")) {
            str = parama.getColumnName() + " numeric(" + parama.getColumnSize() + "," + parama.getDecimalDigits() + ") ";
        }
        return str;
    }


    public String getCommentSql(a columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + "'";
    }


    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }


    public String countIndex(String indexName, String tableName) {
        return "SELECT count(*) FROM pg_indexes WHERE indexname = '" + indexName + "' and tablename = '" + tableName + "'";
    }
}
